package com.ideal.frame.util;

import cn.hutool.extra.ftp.Ftp;
import com.ideal.frame.config.FtpProperties;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

@Slf4j
public class FtpUtil {

    private String host;
    private int port;
    private String username;
    private String password;

    public FtpUtil(FtpProperties ftpProperties) {
        this.host = ftpProperties.getHost();
        this.port = ftpProperties.getPort();
        this.username = ftpProperties.getUsername();
        this.password = ftpProperties.getPassword();
    }

    /**
     * 上传
     *
     * @param path 上传服务器的路径
     * @param file 上传的文件
     * @return
     */
    public boolean upload(String path, File file) {
        Ftp ftp = new Ftp(host, port, username, password);
        try {
            return ftp.upload(path, file);
        } finally {
            try {
                ftp.close();
            } catch (IOException e) {
                log.error("Ftp上传异常", e);
            }
        }
    }
}
